<template>
    <div class="industry">
        <!--        添加/修改行业-->
        <el-dialog
                class="dialog-center"
                :title="addForm.id?'修改行业':'新增行业'"
                :visible.sync="addForm.visible"
                :close-on-click-modal="false"
                width="30%">
            <el-form :model="addForm" :rules="rules" ref="addForm" label-width="150px">
                <el-form-item label="行业名称" prop="name" style="text-align:left">
                    <el-input v-model="addForm.name" size="mini" style="width:70%;"
                              placeholder="行业名称"></el-input>
                </el-form-item>
            </el-form>
            <div align="center">
                <el-button type="primary" @click="submitForm('addForm')" size="small">{{addForm.id?'提交修改':'立即创建'}}
                </el-button>
                <el-button @click="resetForm('addForm')" size="small">重置</el-button>
            </div>

        </el-dialog>
        <el-row type="flex" justify="start">
            <el-col :span="24" style="text-align:left">
                <el-form :inline="true" :model="searchParam" class="demo-form-inline">
                    <el-form-item label="行业名称">
                        <el-input v-model="searchParam.name" placeholder="行业名称" size="mini"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="searchAllIndustry()">查询
                        </el-button>
                        <el-button type="primary" icon="el-icon-plus" size="mini" @click="showAddIndustryDialog()">添加行业
                        </el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="24">
                <el-table
                        ref="multipleTable"
                        :data="tableData"
                        tooltip-effect="dark"
                        style="width: 100%">
                    <el-table-column
                            prop="name"
                            label="行业名称"
                            align="center"
                            show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            label="状态"
                            align="center"
                            show-overflow-tooltip>
                        <template slot-scope="scope">
                            <span :class="scope.row.enable?'c-0f65fa':'c-e09017'">{{scope.row.enable?'正常':'已禁用'}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" min-width="150" fixed="right" align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" :type="scope.row.enable?'warning':'info'"
                                       @click="forbidden(scope.row)">
                                {{scope.row.enable?'禁用':'解禁'}}
                            </el-button>
                            <el-button size="mini" type="primary" @click="modifyIndustry(scope.row)">修改
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="t-a-c m-t-5">
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="searchParam.page"
                            :page-sizes="[10, 20, 30, 40]"
                            :page-size="searchParam.size"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="searchParam.total">
                    </el-pagination>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    // 行业
    import {searchIndustryPageList, addIndustry, updateIndustry} from "@/api/industryApi";
    import CommonUtils from "@/utils/CommonUtils";

    export default {
        name: "industry",
        data() {
            return {
                searchParam: {
                    name: null,
                    page: 1,
                    size: 10,
                    total: 0
                },
                tableData: [],
                addForm: {
                    visible: false,
                    id: null,
                    name: null,
                    enable: true,
                    sort: 0
                },
                rules: {
                    name: [{required: true, message: '请输入行业名称', trigger: 'blur'}],
                }

            }
        },
        created() {
            this.searchAllIndustry();
        },
        methods: {
            //添加行业dialog
            showAddIndustryDialog() {
                this.addForm = CommonUtils.clearObjectValue(this.addForm);
                this.addForm.visible = true;
            },
            //查询行业列表
            searchAllIndustry() {
                searchIndustryPageList(this.searchParam).then(res => {
                    this.tableData = res.list;
                    this.searchParam.total = res.total;
                })
            },
            //分页
            handleSizeChange(val) {
                this.searchParam.size = val;
                this.searchAllIndustry();
            },
            handleCurrentChange(val) {
                this.searchParam.page = val;
                this.searchAllIndustry();
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            //新增
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.addForm.id ? this.updateOneIndustry() : this.addOneIndustry();

                    } else {
                        this.$message.warning("请将信息填写完整");
                    }
                });
            },
            //禁用/解禁
            forbidden(row) {
                this.$confirm(`是否确定${row.enable ? '禁用' : '解除禁用'}行业？`, '重要提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.addForm = CommonUtils.setValueFromOtherObjectByKey(this.addForm, row);
                    this.addForm.enable = !row.enable;
                    this.updateOneIndustry();
                }).catch(() => {
                })
            },

            //显示修改dialog
            modifyIndustry(row) {
                this.addForm = CommonUtils.setValueFromOtherObjectByKey(this.addForm, row);
                this.addForm.visible = true;
            },
            //修改
            updateOneIndustry() {
                updateIndustry(this.addForm).then(res => {
                    this.$message.success('操作成功');
                    this.addForm.visible = false;
                    this.searchAllIndustry();
                })

            },
            //新增
            addOneIndustry() {
                addIndustry(this.addForm).then(res => {
                    this.$message.success('新增行业成功');
                    this.addForm.visible = false;
                    this.searchAllIndustry();
                })
            },
        }
    }
</script>

<style scoped>
    .industry {

    }

</style>